﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.woodsoft.core;
using EQ1.Elements;
using System.Xml;
using com.woodsoft.core.Files;
using EQ1.Objects;

namespace EQ1.Controls {
  public class Page : Control , IUIElement<Page> {
    public Page ( UIDocument doc , string name )
      : base( doc , name ) {
    }
    public Page ( UIDocument doc )
      : this( doc , "Page" ) {
    }

    #region IUIElement<Page> Members

    public void Validate ( Page val , string name ) {
      UIClass par = ( UIClass ) this.ParentNode;

      if( val.LocalName != name ) {
        Page el = new Page( ( UIDocument ) val.OwnerDocument , name );
        el.Copy( val );
        val = el;
      }

      if( par.GetElement( name ) != null )
        par.RemoveElement( name );

      par.AppendChild( val );
    }

    public void Copy ( Page val ) {
      base.Copy( val );

      if( !string.IsNullOrEmpty( val.TabText ) )
        this.TabText = val.TabText;
      if( val.TabTextColor != null )
        this.TabTextColor = val.TabTextColor;
      if( val.TabTextActiveColor != null )
        this.TabTextActiveColor = val.TabTextActiveColor;
      if( !string.IsNullOrEmpty( val.TabIcon ) )
        this.TabIcon = val.TabIcon;
      if( !string.IsNullOrEmpty( val.TabIconActive ) )
        this.TabIconActive = val.TabIconActive;
      if( this.Pieces != null )
        this.Pieces = val.Pieces;
    }

    #endregion

    public string TabText {
      get {
        return this.GetElement( "TabText" ).InnerText;
      }
      set {
        this.Validate( "TabText" , value );
      }
    }
    public RGB TabTextColor {
      get {
        return ( RGB ) this.GetElement( "TabTextColor" );
      }
      set {
        this.TabTextColor.Validate( value , "TabTextColor" );
      }
    }
    public RGB TabTextActiveColor {
      get {
        return ( RGB ) this.GetElement( "TabTextActiveColor" );
      }
      set {
        this.TabTextColor.Validate( value , "TabTextActiveColor" );
      }
    }
    public string TabIcon {
      get {
        return this.GetElement( "TabIcon" ).InnerText;
      }
      set {
        this.Validate( "TabIcon" , value );
      }
    }
    public string TabIconActive {
      get {
        return this.GetElement( "TabIconActive" ).InnerText;
      }
      set {
        this.Validate( "TabIconActive" , value );
      }
    }
    public XmlNodeList Pieces {
      get {
        return this.GetElementsByTagName( "Pieces" );
      }
      set {
        foreach( XmlNode nd in value ) {
          if( nd.LocalName == "Pieces" ) {
            this.AddPiece( nd );
          }
        }
      }
    }
   
    public void AddPiece ( XmlNode val ) {
      if( !string.IsNullOrEmpty( val.InnerText ) ) {
        for( int i = this.Pieces.Count - 1 ; i >= 0 ; i-- ) {
          if( val.InnerText == this.Pieces[i].InnerText ) {
            return;
          }
        }

        if( val.LocalName == "Pieces" )
          this.AppendChild( val );
        else
          this.AddElement( "Pieces" , val.InnerText );
      }
    }
  }
}
